package com.ruoyi.manager.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.manager.domain.*;
import com.ruoyi.manager.mapper.*;
import com.ruoyi.manager.to.SoldierBaseInfoTo;
import com.ruoyi.manager.to.SoldierFullMessageDto;
import com.ruoyi.manager.vo.SoldierFullMessageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.manager.service.ISoldierBaseInfoService;

/**
 * 退役军人Service业务层处理
 *
 * @author Allen
 * @date 2021-08-30
 */
@Service
public class SoldierBaseInfoServiceImpl extends ServiceImpl<SoldierBaseInfoMapper, SoldierBaseInfo> implements ISoldierBaseInfoService {
    @Autowired
    private SoldierBaseInfoMapper soldierBaseInfoMapper;
    @Autowired
    private SoldierSpecialCareInfoMapper soldierSpecialCareInfoMapper;
    @Autowired
    private SoldierArrangeInfoMapper soldierArrangeInfoMapper;
    @Autowired
    private SoldierEmploymentInfoMapper soldierEmploymentInfoMapper;
    @Autowired
    private CityMapper cityMapper;

    /**
     * 查询退役军人
     *
     * @param id
     *         退役军人主键
     *
     * @return 退役军人
     */
    @Override
    public SoldierBaseInfo selectSoldierBaseInfoById(Long id) {
        return soldierBaseInfoMapper.selectSoldierBaseInfoById(id);
    }

    /**
     * 查询退役军人列表
     *
     * @param soldierBaseInfo
     *         退役军人
     *
     * @return 退役军人
     */
    @Override
    public List<SoldierBaseInfo> selectSoldierBaseInfoList(SoldierBaseInfo soldierBaseInfo) {

        List<SoldierBaseInfo> soldierBaseList = soldierBaseInfoMapper.selectSoldierBaseInfoList(soldierBaseInfo);

        soldierBaseList.forEach(x -> {
            String addressBirthId = x.getAddressBirthId();
            if (StringUtils.isNotEmpty(addressBirthId)) {
                List<String> birthIdList = Arrays.asList(addressBirthId.split(","));
                List<City> cityList = cityMapper.selectList(new QueryWrapper<City>().lambda().in(City::getId, birthIdList));
                String join = "";
                for (City city : cityList) {
                    join += city.getCityName() + " ";
                }
                x.setAddressBirth(join + x.getAddressBirth());
            }
            changeBaseNumToStr(x);
        });
        return soldierBaseList;
    }

    @Override
    public void changeBaseNumToStr(SoldierBaseInfo soldierBaseInfo) {
        soldierBaseInfo.setSexStr(DictUtils.getDictLabel("sys_user_sex", soldierBaseInfo.getSex().toString()));
        soldierBaseInfo.setEducationalLevelStr(DictUtils.getDictLabel("degree_of_education", soldierBaseInfo.getEducationalLevel().toString()));
        soldierBaseInfo.setMarryTypeStr(DictUtils.getDictLabel("marry_type", soldierBaseInfo.getMarryType().toString()));
        soldierBaseInfo.setPoliticsStatusStr(DictUtils.getDictLabel("political_status", soldierBaseInfo.getPoliticsStatus().toString()));
        soldierBaseInfo.setHangHonourCardStr(DictUtils.getDictLabel("yes_or_no", soldierBaseInfo.getHangHonourCard().toString()));
    }

    /**
     * 新增退役军人
     *
     * @param soldierBaseInfo
     *         退役军人
     *
     * @return 结果
     */
    @Override
    public int insertSoldierBaseInfo(SoldierBaseInfo soldierBaseInfo) {
        return soldierBaseInfoMapper.insertSoldierBaseInfo(soldierBaseInfo);
    }

    /**
     * 修改退役军人
     *
     * @param soldierBaseInfo
     *         退役军人
     *
     * @return 结果
     */
    @Override
    public int updateSoldierBaseInfo(SoldierBaseInfo soldierBaseInfo) {
        return soldierBaseInfoMapper.updateSoldierBaseInfo(soldierBaseInfo);
    }

    /**
     * 批量删除退役军人
     *
     * @param ids
     *         需要删除的退役军人主键
     *
     * @return 结果
     */
    @Override
    public int deleteSoldierBaseInfoByIds(Long[] ids) {
        return soldierBaseInfoMapper.deleteSoldierBaseInfoByIds(ids);
    }

    /**
     * 删除退役军人信息
     *
     * @param id
     *         退役军人主键
     *
     * @return 结果
     */
    @Override
    public int deleteSoldierBaseInfoById(Long id) {
        return soldierBaseInfoMapper.deleteSoldierBaseInfoById(id);
    }

    @Override
    public SoldierFullMessageVo getSoldierFullMessage(String idNumber) {
        if (StringUtils.isEmpty(idNumber)) {
            return null;
        }
        SoldierBaseInfo soldierBaseInfo = soldierBaseInfoMapper.selectOne(new QueryWrapper<SoldierBaseInfo>().lambda().eq(SoldierBaseInfo::getIdNumber, idNumber));
        if (soldierBaseInfo == null) {
            return null;
        }
        changeBaseNumToStr(soldierBaseInfo);

        Long soldierId = soldierBaseInfo.getId();
        SoldierSpecialCareInfo soldierSpecialCareInfo = soldierSpecialCareInfoMapper.selectOne(new QueryWrapper<SoldierSpecialCareInfo>().lambda().eq(SoldierSpecialCareInfo::getSoldierId, soldierId));


        SoldierArrangeInfo soldierArrangeInfo = soldierArrangeInfoMapper.selectOne(new QueryWrapper<SoldierArrangeInfo>().lambda().eq(SoldierArrangeInfo::getSoldierId, soldierId));
        SoldierEmploymentInfo soldierEmploymentInfo = soldierEmploymentInfoMapper.selectOne(new QueryWrapper<SoldierEmploymentInfo>().lambda().eq(SoldierEmploymentInfo::getSoldierId, soldierId));

        SoldierFullMessageVo fullMessageVo = new SoldierFullMessageVo();
        fullMessageVo.setSoldierBaseInfo(soldierBaseInfo);
        fullMessageVo.setSoldierArrangeInfo(soldierArrangeInfo);
        fullMessageVo.setSoldierEmploymentInfo(soldierEmploymentInfo);
        fullMessageVo.setSoldierSpecialCareInfo(soldierSpecialCareInfo);
        return fullMessageVo;
    }

    @Override
    public int addSoldierFullMessage(SoldierFullMessageDto soldierFullMessageDto) {
        return 0;
    }

    @Override
    public void importBaseInfo(List<SoldierBaseInfoTo> baseInfoList) {
        if (StringUtils.isEmpty(baseInfoList)) {
            throw new ServiceException("导入数据不能为空！");
        }

        List<SoldierBaseInfo> updateList = new ArrayList<>();
        List<SoldierBaseInfo> addList = new ArrayList<>();

        for (SoldierBaseInfoTo baseInfoTo : baseInfoList) {
            String idNumber = baseInfoTo.getIdNumber();
            SoldierBaseInfo soldierBaseInfo = baseMapper.selectOne(new QueryWrapper<SoldierBaseInfo>().lambda().eq(SoldierBaseInfo::getIdNumber, idNumber));

            SoldierBaseInfo baseInfo = new SoldierBaseInfo();
            BeanUtils.copyProperties(baseInfo,baseInfoTo);
            if (null != soldierBaseInfo){
                // 更新
                updateList.add(baseInfo);
            }else {
                //新增
                addList.add(baseInfo);
            }
        }
        if (CollectionUtil.isNotEmpty(addList)){
            baseMapper.insertBatch(addList);
        }
        if (CollectionUtil.isNotEmpty(updateList)){
            baseMapper.updateBatch(updateList);
        }
    }
}
